/*
 * 文 件 名:  DispatchAction.java
 * 版    权:  Tydic Technologies Co., Ltd. Copyright 1993-2013,  All rights reserved
 * 描    述:  <描述>
 * 修 改 人:  yanglong
 * 修改时间:  2013-11-10
 * 跟踪单号:  <需求跟踪单号>
 * 修改单号:  <需求修改单号>
 * 修改内容:  <修改内容>
 */
package com.hfut.jpoa.action;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hfut.jpoa.common.action.AbsAction;
import com.hfut.jpoa.common.dto.Pagination;
import com.hfut.jpoa.common.dto.ResultDto;
import com.hfut.jpoa.common.util.ConstantUtil;
import com.hfut.jpoa.common.util.DateUtils;
import com.hfut.jpoa.dto.WorkSystemDTO;
import com.hfut.jpoa.model.Notice;
import com.hfut.jpoa.model.Syspara;
import com.hfut.jpoa.model.User;
import com.hfut.jpoa.model.WorkSystem;
import com.hfut.jpoa.service.CustomerService;
import com.hfut.jpoa.service.NoticeService;
import com.hfut.jpoa.service.SysparaService;
import com.hfut.jpoa.service.UserService;
import com.hfut.jpoa.service.WorkSystemService;
import com.hfut.jpoa.service.exception.SysparaException;
import com.hfut.jpoa.service.exception.UserException;
import com.hfut.jpoa.service.exception.WorkSystemException;

/**
 * 活动提醒
 * 
 * @author yanglong
 * @version [1.0, 2013-11-10]
 * @since [产品/1.0]
 */
public class WorkSystemAction extends AbsAction<WorkSystemDTO>
{

	private static final Logger LOGGER = LoggerFactory
	        .getLogger(WorkSystemAction.class);

	private static final long serialVersionUID = -7819502638545092791L;

	private WorkSystemService workSystemService;

	private WorkSystem workSystem;

	private List<WorkSystem> lstwWorkSystems;

	/**
	 * 标志位：0-增加 1-修改 2-详情
	 */
	private String actionType;

	/**
	 * 页面类型：0：调度中心 1：售后部
	 */
	private String pageType;

	/**
	 * 包含分页接受前台变量
	 */
	private WorkSystemDTO workSystemDTO;

	/**
	 * 分页结果集
	 */
	private ResultDto<WorkSystem> resultDto;

	/**
	 * 总页数
	 */
	private int totalPage;

	/**
	 * 用户列表
	 */
	private List<User> lstUsers;

	/**
	 * 创建者,以后从session中取
	 */
	private String creator = "yl";

	/**
	 * 用户服务
	 */
	private UserService userService;

	/**
	 * 字典服务
	 */
	private SysparaService sysparaService;

	/**
	 * 客户服务
	 */
	private CustomerService customerService;

	/**
	 * 字典服务
	 */
	private NoticeService noticeService;

	/**
	 * 路线列表
	 */
	private List<Syspara> lstDepart;

	/**
	 * 通知提醒
	 */
	private Notice notice;

	/**
	 * 加载选择项目弹出层
	 * 
	 * @return
	 */
	public String initDialog() throws WorkSystemException
	{
		initDate();
		return "init_dialog_list";
	}

	public String listDialog() throws WorkSystemException
	{

		HttpServletRequest request = ServletActionContext.getRequest();
		String currentPage = request.getParameter("currentPage") == null ? ""
		        : request.getParameter("currentPage");
		Pagination pagination = new Pagination(Integer.parseInt(currentPage));
		String pagesize = request.getParameter("pageSize");
		pagination.setPageSize(Integer.parseInt(pagesize));
		if (workSystemDTO == null)
		{
			workSystemDTO = new WorkSystemDTO();
		}
		workSystemDTO.setPagination(pagination);
		resultDto = workSystemService.queryInfoForPage(workSystemDTO);

		totalPage = resultDto.getPagination().getTotalPage();

		return "dispatch_dialog_list";
	}

	/**
	 * 初始化部门列表
	 * 
	 * @return
	 * @throws WorkSystemException
	 */
	private List<Syspara> findLstDepart() throws WorkSystemException
	{
		// 从字典表中加载初始化部门列表
		List<Syspara> lstDepartTemp = new ArrayList<Syspara>();
		try
		{
			Syspara syspara = new Syspara();
			syspara.setType(ConstantUtil.DEPARTMENT);
			lstDepartTemp = sysparaService.querySysparaList(syspara);
		} catch (SysparaException e)
		{
			LOGGER.error("初始化部门列表异常：", e);
			throw new WorkSystemException("初始化部门列表异常：", e);
		}
		return lstDepartTemp;
	}

	/**
	 * 加载列表页显示数据
	 */
	private void initDate() throws WorkSystemException
	{
		WorkSystemDTO dto = new WorkSystemDTO();
		Pagination pagination = new Pagination(0);
		dto.setPagination(pagination);
		resultDto = workSystemService.queryInfoForPage(dto);
		totalPage = resultDto.getPagination().getTotalPage();
	}

	/**
	 * 加载分页列表
	 * 
	 * @return
	 * @throws WorkSystemException
	 */
	public String listInit() throws WorkSystemException
	{
		// 跳转页面
		return "list_init";
	}

	public String toCenterList() throws WorkSystemException
	{
		listDate();
		actionType = ConstantUtil.ACTIONTYPE_CENTER;
		return "list";
	}

	/**
	 * 加载列表数据
	 * 
	 * @return
	 */
	public String list() throws WorkSystemException
	{
		listDate();
		actionType = ConstantUtil.ACTIONTYPE_LIST;
		return "list";
	}

	private void listDate() throws WorkSystemException
	{

		HttpServletRequest request = ServletActionContext.getRequest();
		String currentPage = request.getParameter("currentPage");
		Pagination pagination = new Pagination(Integer.parseInt(currentPage));
		String pagesize = request.getParameter("pageSize");
		pagination.setPageSize(Integer.parseInt(pagesize));
		if (workSystemDTO == null)
		{
			workSystemDTO = new WorkSystemDTO();
		}
		workSystemDTO.setPagination(pagination);
		resultDto = workSystemService.queryInfoForPage(workSystemDTO);

		List<WorkSystem> result = resultDto.getResult();

		// 加载用户列表
		List<User> initLstUser = initLstUser();
		// 加载部门列表
		List<Syspara> lstDepart = findLstDepart();

		for (int i = 0; i < result.size(); i++)
		{
			// 用户名
			for (int j = 0; j < initLstUser.size(); j++)
			{
				if (StringUtils.equals(result.get(i).getCreator(), initLstUser
				        .get(j).getUserCode()))
				{
					result.get(i).setCreatorName(initLstUser.get(j).getName());
				}
				if (StringUtils.equals(result.get(i).getUpdatePeople(),
				        initLstUser.get(j).getUserCode()))
				{
					result.get(i).setUpdatePeopleName(
					        initLstUser.get(j).getName());
				}

			}
			// 部门名称
			for (int j = 0; j < lstDepart.size(); j++)
			{
				if (StringUtils.equals(result.get(i).getDepart(), lstDepart
				        .get(j).getCode()))
				{
					result.get(i).setDepartName(lstDepart.get(j).getValue());
				}

			}

		}

		totalPage = resultDto.getPagination().getTotalPage();

	}

	/**
	 * 加载用户列表
	 * 
	 * @param user
	 * @return
	 * @throws WorkSystemException
	 */
	public List<User> initLstUser() throws WorkSystemException
	{
		// 加载员工

		User user = new User();

		List<User> lstUser;
		try
		{
			lstUser = userService.queryUser(user);
		} catch (UserException e)
		{
			LOGGER.error("加载员工异常：", e);
			throw new WorkSystemException("加载员工异常：", e);
		}
		return lstUser;
	}

	/**
	 * 新增页面初始化
	 * 
	 * @return
	 */
	public String addInit() throws WorkSystemException
	{
		actionType = ConstantUtil.ACTIONTYPE_ADD;
		return "add_init";
	}

	/**
	 * 新增保存
	 * 
	 * @return
	 * @throws ParseException
	 */
	public String add() throws WorkSystemException, ParseException
	{
		Date nowDate = DateUtils.getNowDate();
		workSystem.setLastUpdateDate(nowDate);
		workSystem.setUpdatePeople(creator);
		workSystem.setCreateDate(nowDate);
		workSystem.setCreator(creator);
		workSystemService.saveWorkSystem(workSystem);

		return "list_init";
	}

	/**
	 * 管理中处理页面初始化
	 */
	public String processInit() throws WorkSystemException
	{

		actionType = ConstantUtil.ACTIONTYPE_CENTER;
		workSystem = workSystemService.findInfo(workSystem);

		return "add_init";
	}

	/**
	 * 修改提醒状态
	 * 
	 * @return
	 * @throws WorkSystemException
	 * @throws IOException
	 */
	public void processNotice() throws WorkSystemException, IOException
	{
		if (null == workSystem)
		{
			failure("系统异常，请联系管理员！");
		}

		workSystemService.update(workSystem);
		success("操作成功！");
	}

	/**
	 * 修改页面初始化
	 */
	public String updateInit() throws WorkSystemException
	{

		actionType = ConstantUtil.ACTIONTYPE_UPDATE;
		WorkSystem resultDispatch = workSystemService.findInfo(workSystem);
		workSystem = resultDispatch;

		return "add_init";
	}

	/**
	 * 修改
	 * 
	 * @return
	 * @throws WorkSystemException
	 * @throws ParseException
	 */
	public String update() throws WorkSystemException, ParseException
	{
		Date nowDate = DateUtils.getNowDate();
		workSystem.setLastUpdateDate(nowDate);
		workSystem.setUpdatePeople(creator);
		workSystemService.update(workSystem);
		return "list_init";
	}

	/**
	 * 删除
	 * @throws IOException 
	 */
	public void delete() throws WorkSystemException, IOException
	{
		if (workSystemService.delete(workSystem) > 0)
		{
			success("操作成功！");
		} else
		{
			failure("操作失败，请联系管理员！");
		}
	}

	public String getActionType()
	{
		return actionType;
	}

	public void setActionType(String actionType)
	{
		this.actionType = actionType;
	}

	public int getTotalPage()
	{
		return totalPage;
	}

	public void setTotalPage(int totalPage)
	{
		this.totalPage = totalPage;
	}

	public UserService getUserService()
	{
		return userService;
	}

	public void setUserService(UserService userService)
	{
		this.userService = userService;
	}

	public List<User> getLstUsers()
	{
		try
		{
			lstUsers = initLstUser();
		} catch (WorkSystemException e)
		{
			LOGGER.error("查询用户异常：", e);
		}
		return lstUsers;
	}

	public void setLstUsers(List<User> lstUsers)
	{
		this.lstUsers = lstUsers;
	}

	public String getPageType()
	{
		return pageType;
	}

	public void setPageType(String pageType)
	{
		this.pageType = pageType;
	}

	public CustomerService getCustomerService()
	{
		return customerService;
	}

	public void setCustomerService(CustomerService customerService)
	{
		this.customerService = customerService;
	}

	public NoticeService getNoticeService()
	{
		return noticeService;
	}

	public void setNoticeService(NoticeService noticeService)
	{
		this.noticeService = noticeService;
	}

	public List<Syspara> getLstDepart()
	{
		try
		{
			lstDepart = findLstDepart();
		} catch (WorkSystemException e)
		{
			LOGGER.error("查询部门异常：", e);
		}
		return lstDepart;
	}

	public void setLstDepart(List<Syspara> lstDepart)
	{
		this.lstDepart = lstDepart;
	}

	public SysparaService getSysparaService()
	{
		return sysparaService;
	}

	public void setSysparaService(SysparaService sysparaService)
	{
		this.sysparaService = sysparaService;
	}

	public Notice getNotice()
	{
		return notice;
	}

	public void setNotice(Notice notice)
	{
		this.notice = notice;
	}

	public WorkSystemService getWorkSystemService()
	{
		return workSystemService;
	}

	public void setWorkSystemService(WorkSystemService workSystemService)
	{
		this.workSystemService = workSystemService;
	}

	public WorkSystem getWorkSystem()
	{
		return workSystem;
	}

	public void setWorkSystem(WorkSystem workSystem)
	{
		this.workSystem = workSystem;
	}

	public List<WorkSystem> getLstwWorkSystems()
	{
		return lstwWorkSystems;
	}

	public void setLstwWorkSystems(List<WorkSystem> lstwWorkSystems)
	{
		this.lstwWorkSystems = lstwWorkSystems;
	}

	public WorkSystemDTO getWorkSystemDTO()
	{
		return workSystemDTO;
	}

	public void setWorkSystemDTO(WorkSystemDTO workSystemDTO)
	{
		this.workSystemDTO = workSystemDTO;
	}

	public ResultDto<WorkSystem> getResultDto()
	{
		return resultDto;
	}

	public void setResultDto(ResultDto<WorkSystem> resultDto)
	{
		this.resultDto = resultDto;
	}

}
